import {
    createSlice
} from "@reduxjs/toolkit";


const taskReducer = createSlice({
    name: 'task',
    initialState: {
        tesk: []
    },
    reducers: {
        initTesk: (prev, {payload}) => {
            prev.tesk = payload
        },

        delet:(prev,{payload})=>{
            prev.tesk = prev.tesk.filter(item =>payload !== item.id?item:'')
            
        },

        checking: (prev, {payload}) => {
            prev.tesk=  prev.tesk.map(item => {
                if (payload === item.id) {
                    item.check = !item.check
                }
                return item
            })
             
        },

        addTesk:(prev, {payload})=>{
            prev.tesk.unshift(payload)
        },

        allTesk:(prev, {payload})=>{
            prev.tesk.map(item=>item.check=payload)
            return prev
        },
        deleteAll:(prev, {payload})=>{
            prev.tesk = prev.tesk.filter(item=>!(item.check))
        }
    }
})

export default taskReducer.reducer
export const {
    initTesk,
    checking,
    delet,
    addTesk,
    allTesk,
    deleteAll
} = taskReducer.actions